<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User_model extends MY_Model {

    function __construct() {
        parent::__construct("user", "id", Array(
            "display_pic" => "Image_model"
        ));
    }

    protected function read($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $results = parent::read($where, $fetchForeign, $depth, $page, $limit);

        foreach ($results as $result) {
            $result->groups = $this->fetchGroups($result->id);
        }

        return $results;
    }

    public function fetchLogin($email, $pwd) {
        $user = parent::findAll("REPLACE(LOWER(email), ' ', '') LIKE REPLACE(LOWER('" . $email . "'), ' ', '')", true, 1);
        if (count($user) > 0) {
            if ($pwd == $this->encrypt->decode($user[0]->password)) {
                $userData = new StdClass();
                $userData->id = $user[0]->id;
                $userData->email = $user[0]->email;
                $userData->display_name = $user[0]->display_name;
                $userData->display_pic = $user[0]->display_pic;
                $userData->fb_id = $user[0]->fb_id;
                $userData->register_date = $user[0]->register_date;
                return $userData;
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
    }

    public function fetchRoles($userId) {
        $this->load->model("Role_model");
        $this->load->model("User_role_model");
        $this->load->model("User_group_model");
        $this->load->model("Group_role_model");

        $roleArr = Array();

        // check super-admin
        $isSuperAdmin = false;
        if (count($this->User_role_model->findAll(Array("user_id" => $userId, "role_code" => "super_admin"))) > 0) {
            // is super admin by user
            $isSuperAdmin = true;
        } else {
            foreach ($this->User_group_model->findAll(Array("user_id" => $userId)) as $userGroup) {
                if (count($this->Group_role_model->findAll(Array("group_id" => $userGroup->group_id, "role_code" => "super_admin"))) > 0) {
                    // is super admin by group
                    $isSuperAdmin = true;
                    break;
                }
            }
        }
        if ($isSuperAdmin) {
            $excRoles = Array("super_admin");
            $qArr = "code NOT IN (";
            $i = 0;
            foreach ($excRoles as $role_code) {
                $qArr .= "'" . $role_code . "'";
                if ($i < count($excRoles) - 1) {
                    echo ",";
                };
                $i++;
            }
            $qArr .= ")";
            foreach ($this->Role_model->read($qArr) as $role) {
                array_push($roleArr, $role->code);
            }
            array_push($roleArr, "pj_contributor");
        } else {
            // from user_role
            foreach ($this->User_role_model->findAll(Array("user_id" => $userId)) as $user_role) {
                array_push($roleArr, $user_role->role_code);
            }
            // from group_role
            $this->db->select("group_role.role_code as role_code");
            $this->db->from("user_group, group_role");
            $this->db->where("user_group.user_id", $userId);
            $this->db->where("group_role.group_id = user_group.group_id");
            $query = $this->db->get();
            foreach ($query->result() as $result) {
                array_push($roleArr, $result->role_code);
            }
        }
        // apply pj contributor if no roles
        if ((count($roleArr) < 1) && ($this->User_model->findById($userId) != NULL)) {
            array_push($roleArr, "pj_contributor");
        }
        // everyone can upload to gallery ***
        array_push($roleArr, "gal_contributor");

        return $roleArr;
    }

    public function fetchGroups($userId) {
        $groupArr = Array();
        $this->load->model("User_group_model");
        $this->load->model("Group_model");

        foreach ($this->User_group_model->findAll(Array("user_id" => $userId)) as $userGroup) {
            $group = $this->Group_model->findById($userGroup->group_id);
            if ($group != NULL) {
                array_push($groupArr, $group);
            }
        }

        return $groupArr;
    }

    public function fetchDetail($userId) {
        $detail = NULL;

        return $detail;
    }

    public function fetchUsersInGroup($groupNames, $order = "id", $page = -1, $limit = 5) {
        $result = Array();
        $this->db->select("user.id as id");
        $this->db->from("user, group, user_group");
        $this->db->where("user.id = user_group.user_id");
        $this->db->where("group.id = user_group.group_id");
        $this->db->where("group.name IN (" . $groupNames . ")");
        if ($page >= 0) {
            $this->db->limit($limit, ($page * $limit));
        }
        switch ($order) {
            case "id" :
                $this->db->order_by("user.id", "asc");
                break;
            case "rand" :
                $this->db->order_by("user.id", "random");
                break;
        }
        $query = $this->db->get();
        foreach ($query->result() as $row) {
            $user = $this->User_model->findById($row->id, true, 2);
            if ($user != NULL) {
                array_push($result, $user);
            }
        }
        return $result;
    }

    public function getListRelawan($order = "rand", $page = 0, $limit = 5) {
        $this->db->select("user.id");
        $this->db->select("group.name");
        $this->db->from("user");
        $this->db->from("group");
        $this->db->from("user_group");
        $this->db->where("user.id = user_group.user_id");
        $this->db->where("group.id = user_group.group_id");
        $this->db->where("group.name LIKE 'RELAWAN 2014 REGION%'");
        $this->db->where("user.display_pic > 0");
        switch ($order) {
            case "id" :
                $this->db->order_by("user.id", "asc");
                break;
            case "rand" :
                $this->db->order_by("user.id", "random");
                break;
        }
        $result = Array();
        $this->db->limit($limit, ($page * $limit));
        $query = $this->db->get();
        foreach ($query->result() as $row) {
            $user = $this->User_model->findById($row->id, true, 2);
            if ($user != NULL) {
                $user->group_name = $row->name;
                array_push($result, $user);
            }
        }

        return $result;
    }

}